﻿using System;

using System.Web;
using System.Web.Security;

using System.Security.Principal;


namespace XQ.Common
{
    public class Cookies
    {
        #region Cookies设置
        private static FormsAuthenticationTicket CreateNewTicket(string username, bool isPersistent, string userData)
        {
            DateTime issueTime = DateTime.Now;
            DateTime expirationTime = issueTime.AddMinutes(20);

            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                1
                , username
                , issueTime
                , expirationTime
                , isPersistent
                , userData);
            return ticket;
        }

        private static void AddTicketToResponse(FormsAuthenticationTicket ticket)
        {
            //加密
            string encryptedTicketStr = FormsAuthentication.Encrypt(ticket);
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicketStr);
            //HttpCookie cookie = new HttpCookie("UsersName", encryptedTicketStr);

            //if (ticket.IsPersistent)
            //{
            //    DateTime issueTime = DateTime.Now;
            //    DateTime expirationTime = issueTime.AddDays(365);
            //    cookie.Expires = expirationTime;
            //}

            //cookie.Value = userName;
            System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
        }

        public static void CreateTicket(bool rememberMe, string userName, string userData)
        {
            ////FormsAuthenticationTicket authTicket = CreateNewTicket(userName, rememberMe, "Administrator");
            ////FormsAuthenticationTicket authTicket = CreateNewTicket(userName, rememberMe, userData);
            //FormsAuthenticationTicket authTicket = CreateNewTicket(userName, true, userData);
            //AddTicketToResponse(authTicket);


            //System.Web.HttpContext.Current.Request.Cookies.Remove("COOKIE_NAME_FOR_USER"); 
            //ToDo 登陆成功
            FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
                                1,
                                userName,
                                DateTime.Now,
                                DateTime.Now.AddDays(365),
                                false,
                                userData //写入用户角色
                                );

            string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
            System.Web.HttpCookie authCookie = new System.Web.HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

            DateTime issueTime = DateTime.Now;
            DateTime expirationTime = issueTime.AddDays(365);
            authCookie.Expires = expirationTime;
            System.Web.HttpContext.Current.Response.Cookies.Add(authCookie);
            #region
            //try
            //{
            //    //删除Cookie
            //    System.Web.HttpContext.Current.Request.Cookies.Remove("COOKIE_NAME_FOR_USER");
            //}
            //catch { }

            //System.Web.HttpContext.Current.Response.Cookies["COOKIE_NAME_FOR_USER"].Value = userName;
            //HttpCookie cookie = new HttpCookie("COOKIE_NAME_FOR_USER");
            //cookie.Expires = DateTime.Now.AddDays(365) ;
            ////cookie["COOKIE_NAME_FOR_USER"] = userName;
            //cookie.Value = userName;
            //System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
            #endregion
            //System.Web.HttpContext.Current.Response.Cookies["_userName"].Value = userName;
        }


        #region Cookic测试
        //public void CookieCreate(string COOKIE_NAME_FOR_USER, string COOKIE_USER_NAME)
        //{
        //    HttpCookie cookie = new HttpCookie(COOKIE_NAME_FOR_USER);
        //    cookie.Expires = DateTime.Now.AddYears(1);
        //    cookie[COOKIE_USER_NAME] = "用户名";
        //    System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
        //}

        //public void CookieRead(string COOKIE_NAME_FOR_USER)
        //{
        //    HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies.Get(COOKIE_NAME_FOR_USER);
        //}

        //public void CookieDelete(string COOKIE_NAME_FOR_USER)
        //{
        //    System.Web.HttpContext.Current.Request.Cookies.Remove(COOKIE_NAME_FOR_USER);
        //}
        #endregion
        #endregion
    }
}
